require 'Flight.rb'
require 'Booking.rb'

#
# CPL Assignment 2
# Tim Caljé, Jeroen De Raedt & Jorick Van Aken
#
class BookingDao
  
  def initialize(connection)
    @connection = connection
  end
  
  def getBusinessDetailsForFlight(id, date)
    return getDetailsForFlight(id, date, "B")
  end
  
  def getEconomyDetailsForFlight(id, date)
    return getDetailsForFlight(id, date, "E")
  end
  
  def getFirstClassDetailsForFlight(id, date)
    return getDetailsForFlight(id, date, "F")
  end
  
  def holdFlights(flights,date,persons,type)
    totalResults = Array.new
   begin
    for i in 0..persons.length-1do 
      results = Array.new
      for j in 0..flights.length-1do 
        tmpFlight = flights[j]
        begin
          tmp = holdFlight(tmpFlight,date,type ,persons[i])
          results.push(tmp)
        rescue
          
          raise Exception, "Not all flights could been holded, nothing is holded."
        end  
      end
      totalResults +=results
    end
   rescue Exception
     cancelHolds(totalResults)
    end
    totalResults  
   end
  
  def cancelHolds(bookingIds)
   begin
    for i in 0..bookingIds.length-1do 
      cancelHold(bookingIds[i])
    end
   rescue Exception
    raise Exception, "Not all items could be cancelled."
   end
  end
  
  def cancelBookings(bookings)
   begin
    for i in 0..bookings.length-1do 
      cancelHold(bookings[i].id)
    end
   rescue Exception
    raise Exception, "Not all bookings could be cancelled."
   end
  end

  def bookFlights(bookingIds)
    results = Array.new
   begin
    for i in 0..bookingIds.length-1 do 
      results.push(bookFlight(bookingIds[i]))
    end
   rescue Exception
    raise Exception, "Not all holdings could be booked."
    cancelBookings(results)
   end
   return results
  end
  
  def getBookings(ids)    
    resultList = Array.new
    for i in 0..ids.length-1 do         
      resultList.push(getBooking(ids[i]))
    end
    return resultList
  end
  
  def getBooking(id)    
    results = @connection.query("Q"+id.to_s)
    if results.slice(0..1).to_s == "FN"
          raise Exception, "No such booking-id: #{id}"
    else 
      if results.slice(0..4).to_s == "ERRIM"
          raise Exception, "Ill formated message: #{id}"
      else            
          if results.slice(0..0) == "B"
            status = "Booked"
          else
            status = "Held"
          end
          date = results.slice(1..10)
          time = results.slice(11..15)
          duration = results.slice(16..20)
          flightCode = results.slice(21..26)          
          if results.slice(27..27) == "E"
            seatclass = "Economy"
          elsif results.slice(27..27) == "F"
             seatclass = "First class"
          else
            seatclass = "Business"
          end         
          if results.slice(28..28) == "F"
            gender = "Female"
          else
            gender = "Male"
          end
          fName = results.slice(29..43)
          sName = results.slice(44..63)
          price = results.slice(64..68)         
          return "status: #{status} date: #{date} time #{time} duration: #{duration} flight-code: #{flightCode} seatclass: #{seatclass} gender: #{gender} first name: #{fName.strip} surname: #{sName.strip} price: € #{price.to_i}"
    end
    end
  end
  
#{Status[1]}{Date[10]}{Time[5]}{Duration[5]}{Flight Code[6]}{Class[1]}
#{Gender[1]}{First Name[15]}{Surname[20]}{Euros[5]}
  
  private
  def getDetailsForFlight(id, date, type)
    results = @connection.query("S"+date+id+type)
    return results.slice(0..2).to_i, results.slice(3..7).to_i
  end  
  
  private 
  def holdFlight(flight,date,type,person)
    results = @connection.query("H"+date+flight+type+person.gender+person.firstName+person.surName)
    if results[0].to_s == "S"
      return results.slice(1..32)
    else 
      raise Exception, "Failed holding."
    end
  end 
   
  private 
  def cancelHold(bookingId)
    results = @connection.query("X"+bookingId)
    if results[0].to_s == "F"
      raise Exception, "Failed cancellation."
    end
  end
  
  private 
  def bookFlight(bookingId)
    results = @connection.query("B"+bookingId)
    if results[0].to_s == "F"
      raise Exception, "Failed booking."
    else 
      date = results.slice(1..10)
      time = results.slice(11..15)
      duration = results.slice(16..20)
      flightId = results.slice(21..26)
      type = results.slice(27..27)
      person = Person.new(results.slice(29..43),results.slice(44..63),results.slice(28..28))
      price = results.slice(64..68).to_i
      return Booking.new(bookingId,date,time,duration,flightId,type,person,price)
    end
  end
end